import Vue from "vue";
import VueRouter from "vue-router";
import Main from "../views/Main.vue";
import Categories from "../views/Categories";
import CategoryList from "../views/CategoryList";

import Items from "../views/Items";
import ItemList from "../views/ItemList";

import Heros from "../views/Heros";
import HeroList from "../views/HeroList";

import Articles from "../views/Articles";
import ArticleList from "../views/ArticleList";

import Ads from "../views/Adedit";
import AdsList from "../views/AdList";

import AdminuserEdit from "../views/AdminuserEdit";
import AdminuserList from "../views/AdminuserList";

import GlcenterEdit from "../views/GlcenterEdit";
import GlcenterList from "../views/GlcenterList";

import Shipin from "../views/Shipin.vue";
import ShipinList from "../views/ShipinList.vue";

import login from "../views/Login";

import user from "../views/User.vue";

import welcome from "../views/welcome";
Vue.use(VueRouter);

const routes = [
  { path: "/login", name: "login", component: login, meta: { deng: 123 } },
  {
    path: "/",
    name: "Main",
    component: Main,
    children: [
      { path: "/categories/create", component: Categories },
      { path: "categories/list", component: CategoryList },
      { path: "categories/create/:id", component: Categories, props: true },

      { path: "/items/create", component: Items },
      { path: "/items/list", component: ItemList },
      { path: "/items/create/:id", component: Items, props: true },

      { path: "/heros/create", component: Heros },
      { path: "/heros/list", component: HeroList },
      { path: "/heros/create/:id", component: Heros, props: true },

      { path: "/Articles/create", component: Articles },
      { path: "/Articles/list", component: ArticleList },
      { path: "/Articles/create/:id", component: Articles, props: true },

      { path: "/ads/create", component: Ads },
      { path: "/ads/list", component: AdsList },
      { path: "/ads/create/:id", component: Ads, props: true },

      { path: "/admin_user/create", component: AdminuserEdit },
      { path: "/admin_user/list", component: AdminuserList },
      { path: "/admin_user/create/:id", component: AdminuserEdit, props: true },

      { path: "/glcenter/create", component: GlcenterEdit },
      { path: "/glcenter/list", component: GlcenterList },
      { path: "/glcenter/create/:id", component: GlcenterEdit, props: true },

      { path: "/shipin/create", component: Shipin },
      { path: "/shipin/list", component: ShipinList },
      { path: "/shipin/create/:id", component: Shipin, props: true },

      { path: "/user/list", component: user },

      { path: "welcome", component: welcome }
    ]
  },

  // 路由重定向
  {
    path: "*",
    name: "any",
    redirect: "login"
  }
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes
});

// 路由守卫
// router.beforeEach((to, from, next) => {
//   if (to.path == '/') {
//     if(sessionStorage.token){
//       next()
//     }else{
//       next('/login')
//     }
//   }else {
//     next()
//   }
// })

router.beforeEach((to, from, next) => {
  if (!to.meta.deng == 123 && !sessionStorage.token) {
    next("/login");
  }
  next();
});

export default router;
